package com.tjbank.cssys.web.controller;


import com.tjbank.cssys.domain.CkFeedbackConfigTable;
import com.tjbank.cssys.domain.CkLog;
import com.tjbank.cssys.web.service.CkFeedBackConfigService;
import com.tjbank.cssys.web.service.CkLogService;
import com.tjbank.cssys.web.util.CommonResult;
import com.tjbank.cssys.web.util.GetUserCodeFromToken;
import com.tjbank.cssys.web.util.RedisService;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Map;
import java.util.UUID;

@RequestMapping("/feedBackConfig")
@RestController
@Api(tags = "CkFeedBackConfigController", description = "反馈码设置管理")
public class CkFeedBackConfigController {
    @Autowired
    private CkFeedBackConfigService ckFeedBackConfigService;
    @Autowired
    private RedisService redisService;
    @Autowired
    private CkLogService ckLogService;

    private final static Logger logger = LoggerFactory.getLogger(CkLogController.class);

    /**
     * 查询
     *
     * @return
     */
    @RequestMapping("/selectList")
    private CommonResult selectList(HttpServletRequest request) {
        Map<String, CkFeedbackConfigTable> ckFeedBackConfigTableMap = ckFeedBackConfigService.selectList();
        String userCode = null;
        try {
            userCode = GetUserCodeFromToken.getUserCode(request);
        } catch (Exception e) {
            logger.error("反馈码值查询异常"+e.getMessage());
        }
        CkLog ckLog = new CkLog();
        ckLog.setLogId(UUID.randomUUID().toString());
        ckLog.setLogName(UUID.randomUUID().toString());
        ckLog.setOperationType("反馈码设置管理查询");
        ckLog.setUserId(userCode);
        Date date = new Date();
        ckLog.setTimes(new Date(date.getTime()));
        if (ckFeedBackConfigTableMap != null) {
            ckLog.setResult("success:200");
            ckLogService.addInsert(ckLog);
            return CommonResult.success(ckFeedBackConfigTableMap);
        } else {
            ckLog.setResult("error:500");
            ckLogService.addInsert(ckLog);
            return CommonResult.failed("错误");
        }

    }

    /**
     * 更新
     *
     * @param ckFeedbackConfigTable
     * @return
     */
    @PutMapping("/update")
    private CommonResult update(@RequestBody CkFeedbackConfigTable ckFeedbackConfigTable, HttpServletRequest request) {
        ckFeedBackConfigService.update(ckFeedbackConfigTable);

        String userCode = null;
        try {
            userCode = GetUserCodeFromToken.getUserCode(request);
        } catch (Exception e) {
            logger.error("更新反馈码异常"+e.getMessage());
        }
        CkLog ckLog = new CkLog();
        ckLog.setLogId(UUID.randomUUID().toString());
        ckLog.setUserId(userCode);
        Date date = new Date();
        ckLog.setTimes(new Date(date.getTime()));
        ckLog.setLogName(UUID.randomUUID().toString());
        ckLog.setOperationType("反馈码更新");
        ckLog.setParams(ckFeedbackConfigTable + "");
        ckLog.setResult("success:200");
        ckLogService.addInsert(ckLog);
        return CommonResult.success("");
    }

    @PostMapping("/add")
    private CommonResult add(CkFeedbackConfigTable ckFeedbackConfigTable, HttpServletRequest request) {
        ckFeedBackConfigService.add(ckFeedbackConfigTable);

        String userCode = null;
        try {
            userCode = GetUserCodeFromToken.getUserCode(request);
        } catch (Exception e) {
            logger.error("新增反馈吗异常"+e.getMessage());

        }
        CkLog ckLog = new CkLog();
        ckLog.setLogId(UUID.randomUUID().toString());
        ckLog.setLogName(UUID.randomUUID().toString());
        ckLog.setUserId(userCode);
        Date date = new Date();
        ckLog.setTimes(new Date(date.getTime()));
        ckLog.setParams(ckFeedbackConfigTable + "");
        ckLog.setOperationType("反馈码新增");
        ckLog.setResult("success:200");
        ckLogService.addInsert(ckLog);
        return CommonResult.success("");
    }

}
